/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.service.impl;

import com.opgea.qms.dao.CompanyDAO;
import com.opgea.qms.dao.DestinationDAO;
import com.opgea.qms.domain.entities.Company;
import com.opgea.qms.domain.entities.Destination;
import com.opgea.qms.service.DestinationService;
import com.opgea.qms.web.form.DestinationDTO;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Ramesh
 */
@Service
public class DestinationServiceImpl implements DestinationService{

    
    @Autowired
    private DestinationDAO destinationDAO;
    @Autowired
    private CompanyDAO companyDAO;
    
    @Override
    public DestinationDTO create(DestinationDTO destinationDTO) {
        Company company = companyDAO.find(destinationDTO.getCompanyId());
        Destination destination = new Destination();
        //destination.setId(destinationDTO.getId());
        destination.setName(destinationDTO.getName());
        destination.setCompany(company);
        destinationDAO.create(destination);
        return destinationDTO;
    }

    @Override
    public DestinationDTO update(DestinationDTO destinationDTO) {
        Destination destination = null;
        if(destinationDTO.getId() != null ){
            destination = destinationDAO.find(destinationDTO.getId());
            destination.setName(destinationDTO.getName());
            destinationDAO.update(destination);
        }else{
            destination = new Destination();
            this.create(destinationDTO);
        }
        return destinationDTO;
    }

    @Override
    public DestinationDTO find(DestinationDTO destinationDTO) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<DestinationDTO> findAll() {
        List<Destination> destinationList = destinationDAO.findAll();
        List<DestinationDTO> destDTOList = new ArrayList<DestinationDTO>();
        for(Destination dest: destinationList){
            DestinationDTO dto = new DestinationDTO();
            dto.setCompanyId(dest.getCompany().getId());
            dto.setId(dest.getId());
            dto.setName(dest.getName());
            destDTOList.add(dto);
        }
        return destDTOList;
    }

    @Override
    public List<DestinationDTO> findAllByCompany(Long companyId) {
        List<Destination> destinationList = destinationDAO.findAllByCompany(companyId);
        List<DestinationDTO> destDTOList = new ArrayList<DestinationDTO>();
        for(Destination dest: destinationList){
            DestinationDTO dto = new DestinationDTO();
            dto.setCompanyId(dest.getCompany().getId());
            dto.setId(dest.getId());
            dto.setName(dest.getName());
            destDTOList.add(dto);
        }
        return destDTOList;
    }
    
}
